<?php
/**
*	企业微信
*/
namespace Rainrock\Framework\Chajian;


use Rainrock\Framework\kernel\core\Chajian;
use Rainrock\Framework\kernel\base\Base;




class wxqyChajian extends Chajian{
	
	private $nowact = '';
	
	/**
	*	获取地址
	*/
	protected function getUrl($act, $token='')
	{
		$this->nowact = $act;
		$url = 'https://qyapi.weixin.qq.com/cgi-bin/'.$act.'';
		if($token)$url .= '?access_token='.$token.'';
		return $url;
	}
	
	/**
	*	获取token
	*/
	protected function get_access_token($id='')
	{
		$corpid 	= Base::getConfig('wxqy_corpid');
		$corpsecret = Base::getConfig('wxqy_agentsecret');//应用的secret
		$agentid 	= Base::getConfig('wxqy_agentid'); 
		if(!$corpid || !$corpsecret || !$agentid)return $this->returnerror('未配置企业微信');
		$key		= md5(''.$corpid.'_'.$corpsecret.'');
		$access_token = $this->c('cache')->get($key);
		if($access_token)return $this->returnsuccess($access_token);
		$url 	 = $this->getUrl('gettoken');
		$url 	.= '?corpid='.$corpid.'&corpsecret='.$corpsecret.'';
		$result  = $this->c('curl')->getcurl($url);
		$barr 	 = $this->result($result);
		if(!$barr['success'])return $barr;
		$access_token 	= $barr['data']['access_token'];
		$expires_in 	= $barr['data']['expires_in'];
		$bool = $this->c('cache')->set($key, $access_token, floatval($expires_in)-60);
		if(!$bool)return $this->returnerror('无法使用缓存');
		return $this->returnsuccess($access_token);
	}
	
	/**
	*	结果处理
	*/
	protected function result($str)
	{
		if(!$str)return $this->returnerror('无法联网访问:'.$str.'');
		$barr = json_decode($str, true);
		if(!isset($barr['errcode']) || $barr['errcode']==0)return $this->returnsuccess($barr);
		$code 	= $barr['errcode'];
		$msg 	= $this->resultmsg($code);
		return $this->returnerror(''.$code.':('.$this->nowact.')'.$barr['errmsg'].':'.$msg.'', $code);
	}
	
	/**
	*	错误的信息
	*/
	protected function resultmsg($code)
	{
		$cbarr['a48006'] = '由于企业长时间未使用应用，接口权限被收回，需企业管理员重新启用';
		return $this->arrvalue($cbarr, 'a'.$code);
	}
	
	/**
	*	获取微信状态
	*/
	public function usergetuserid($mobile)
	{
		$barr 	= $this->get_access_token();
		if(!$barr['success'])return $barr;
		$token 	= $barr['data'];
		$url 	= $this->getUrl('user/getuserid', $token);
		$result = $this->c('curl')->postcurl($url, json_encode(array(
			'mobile' => $mobile
		)));
		$barr 	= $this->result($result);
		if(!$barr['success'])return $barr;
		$data 	= $barr['data'];
		$wxuser = $data['userid']; //微信用户名
		
		$barr 	= $this->userget($wxuser, $token);
		if(!$barr['success'])return $barr;
		
		$data 		= $barr['data'];
		$wxstate 	= $data['status']; //获取到状态
		
		return $this->returnsuccess(array(
			'mobile'  => $mobile,
			'wxstate' => $wxstate,
			'wxuser'  => $wxuser,
		));
	}
	
	/**
	*	获取用户信息
	*/
	public function userget($userid, $token)
	{
		if(is_numeric($token)){
			$barr 	= $this->get_access_token($token);
			if(!$barr['success'])return $barr;
			$token  = $barr['data'];
		}
		$url 	= $this->getUrl('user/get', $token).'&userid='.$userid.'';
		$result = $this->c('curl')->getcurl($url);
		$barr 	= $this->result($result);
		return $barr;
	}
	
	/**
	*	发消息
	*/
	public function sendText($touser, $cont)
	{
		$barr 	= $this->get_access_token();
		if(!$barr['success'])return $barr;
		$token 	= $barr['data'];
		$agentid= Base::getConfig('wxqy_agentid');
		$url 	= $this->getUrl('message/send', $token);
		$result = $this->c('curl')->postcurl($url, json_encode(array(
			'touser' => str_replace(',','|', $touser),
			'msgtype'=> 'text',
			'agentid'=> $agentid,
			'text' 	 => array(
				'content' => $cont
			)
		)));
		$barr 	= $this->result($result);
		if(!$barr['success'])return $barr;
		return $this->returnsuccess();
	}
}
	